package com.hornsun.client.mall.commodity.api;

import com.hornsun.client.mall.commodity.dto.OrderDetailDTO;
import com.hornsun.domain.constant.types.OrderType;
import com.hornsun.domain.dbo.*;
import com.hornsun.repository.*;
import com.hornsun.util.Singleton;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/5/2.
 */
@RestController
@RequestMapping("/api/order")
public class OrderAPI {
    @Resource
    private AddressRepository addressRepository;

    @Resource
    private OrderFormRepository orderFormRepository;

    @Resource
    private MemberCouponRepository memberCouponRepository;

    @Resource
    private CommodityRepository commodityRepository;

    @Resource
    private OrderFormDetailRepository orderFormDetailRepository;

    /**
     * 提交订单
     * @param httpRequest
     * @param orderDetailDTO
     * @return
     */
    @Transactional
    @PostMapping("/submitOrder")
    public ResponseEntity submitOrder(HttpServletRequest httpRequest, OrderDetailDTO orderDetailDTO){
        String openid = httpRequest.getAttribute("openid").toString();
        Address address = addressRepository.getAddressByGuid(orderDetailDTO.getAddressGuid());
        Commodity commodity = commodityRepository.getCommodityByGuid(orderDetailDTO.getCommodityGuid());
        String orderGuid = UUID.randomUUID().toString();
        String orderNum = Singleton.getOrderNum();
        //插入订单表
        OrderForm orderForm = new OrderForm();
        orderForm.setOpenid(openid);
        orderForm.setAlias(orderNum);
        orderForm.setActualExpenditure(orderDetailDTO.getActualExpenditure());
        orderForm.setCouponGuid(orderDetailDTO.getMemberCouponGuid());
        orderForm.setDiscounts(orderDetailDTO.getDiscounts());
        orderForm.setLocation(address.getLocation());
        orderForm.setPhone(address.getPhone());
        orderForm.setRecipient(address.getRecipient());
        orderForm.setOrderGuid(orderGuid);
        orderForm.setOrderTime(new Date());
        orderForm.setTransactionStatus((byte)OrderType.UNPAID.getType().intValue());
        orderFormRepository.insertOrderForm(orderForm);
        //更新优惠券使用时间和使用订单号
        if (orderDetailDTO.getMemberCouponGuid()!=null&&!orderDetailDTO.getMemberCouponGuid().isEmpty()) {
            System.out.println(orderDetailDTO.getMemberCouponGuid());
            MemberCoupon memberCoupon = new MemberCoupon();
            memberCoupon.setMemberCouponGuid(orderDetailDTO.getMemberCouponGuid());
            memberCoupon.setOrderNum(orderNum);
            memberCoupon.setPayTime(new Date());
            memberCouponRepository.updateMemberCoupon(memberCoupon);
        }
        //插入订单详情表
        OrderFormDetail orderFormDetail = new OrderFormDetail();
        orderFormDetail.setOrderGuid(orderGuid);
        orderFormDetail.setCommodityGuid(orderDetailDTO.getCommodityGuid());
        orderFormDetail.setCount(orderDetailDTO.getCount());
        orderFormDetail.setDescription(commodity.getDescription());
        orderFormDetail.setImage(commodity.getImage());
        orderFormDetail.setPrice(commodity.getPrice());
        orderFormDetailRepository.insertOrderDetail(orderFormDetail);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 支付成功
     * @param orderGuid
     * @return
     */
    @Transactional
    @PostMapping("/paySuccess")
    public ResponseEntity<?> paySuccess(@RequestParam("guid")String orderGuid){
        //修改订单状态
        orderFormRepository.updateOrderStatus(orderGuid,(byte)OrderType.UNCONFIRMED.getType().intValue());
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 取消订单
     * @param orderGuid
     * @return
     */
    @Transactional
    @PostMapping("/cancelOrder")
    public ResponseEntity<?> cancelOrder(@RequestParam("guid")String orderGuid){
        orderFormRepository.updateOrderStatus(orderGuid,(byte)OrderType.CANCEL.getType().intValue());
        OrderForm orderForm = orderFormRepository.getOrderByOrderGuid(orderGuid);
        if(orderForm.getOrderGuid()!=null){
            memberCouponRepository.updateToUsable(orderForm.getCouponGuid());
        }
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 删除订单
     * @param orderGuid
     * @return
     */
    @Transactional
    @PostMapping("/deleteOrder")
    public ResponseEntity<?> deleteOrder(@RequestParam("guid")String orderGuid){
        orderFormRepository.deleteOrderForm(orderGuid);
        orderFormDetailRepository.deleteOrderDetail(orderGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }




}
